package com.kecheng.com.aop;

import com.kecheng.com.utils.BeanHelper;
import com.kecheng.com.utils.CommonUtil;
import com.kecheng.dao.PeizhiDao;
import com.kecheng.dao.TeamDao;
import com.kecheng.pojo.Team;
import com.kecheng.util.SpringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 切面类：需要放入IoC容器中(@Component)、再声明为一个切面(@Aspect)
 * 优先级：可以使用@Order(1)注解来指定切面的优先级，值越小优先级越高
 * 或 实现Ordered接口, getOrder()方法的返回值越小, 优先级越高
 *
 * @author qiaobc
 */
public class AccessControl implements Filter {
    static Log logger = LogFactory.getLog(AccessControl.class);

    private TeamDao teamDao;

    /**
     * 需求：在com.qiaobc.spring.aop.impl.ArithmeticCalculator接口的所有实现类的每个方法执行前打印日志
     * 前置通知：在目标方法开始之前执行，用@Before来声明
     */
    public void beforeMethod(HttpServletRequest servletRequest) {
        //作微信登录动作
        final HttpSession session = servletRequest.getSession();
        Team team = (Team) session.getAttribute("user");

        if(CommonUtil.isEmpty(teamDao)){
            teamDao=(TeamDao)SpringUtil.getBean("teamDao");
        }

        if (CommonUtil.isEmpty(team)) {//未登录
            //作微信登录动作并写入session,todo 先写死,后面弄微信登录动作
            List<Team> findall = teamDao.findall();
            session.setAttribute("user", findall.get(0));
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {


        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        final String origin = ((HttpServletRequest) request).getHeader("origin");

        if (Boolean.getBoolean("isDevelop")) {
            response.setHeader("Access-Control-Allow-Origin", ((HttpServletRequest) request).getHeader("origin"));
        }
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        response.setHeader("Access-Control-Allow-Headers",
                "Access-Control-Allow-Methods," +
                        "Access-Control-Allow-Origin," +
                        "credentials," +
                        "Content-Type");
        String header = request.getHeader("Content-Type");

        // 执行目标方法
        try {
            if ("OPTIONS".equals(request.getMethod())) {
                return;
            } else {
                //自动登录
                beforeMethod(request);
                filterChain.doFilter(servletRequest, servletResponse);
            }
        } catch (Throwable e) {
            System.out.println("Exception in method" + e);
            throw new RuntimeException(e);
        }
    }

    @Override
    public void destroy() {

    }
}

